<?php 
namespace town\controller;

use town\dmod\DB;
use town\model\Comment;
use town\base\Request;
use town\tool\Page;

/**
 * 帖子信息
 */
class CommentController
{

	/**
	 * 发帖
	 */
	public function send()
	{
		$datas = $_POST;
		$pid   = $_POST['pid'];
		unset($_POST['pid']);

		// 保存到 com_map_posts 表中
		$datas['create_at'] = time();
		$comentID = Comment::save($datas);
		if ($comentID > 0) {
			DB::table('com_map_posts')->insert(['cid'=>$comentID, 'pid'=>$pid]);
		}
		redirect($_SERVER['HTTP_REFERER'], true);
	}


	/**
	 * 评论列表
	 */
	public function comment_l(Request $req)
	{
		$page = $req->get('town_p', 1);
		$page = (($page-1) * 10);
		$count= DB::table('comments')->count();
		$show = (new Page($count))->html();
		
		$list = DB::table('comments')->limit($page .',10')->get();

		foreach ($datas as $key => &$value) {
			$value['create_at'] = date('Y/m/d', $value['create_at']);
			$value['update_at'] = date('Y/m/d', $value['update_at']);
		}
		return view('back/comment_l', ['list'=>$list, 'pagination'=>$show]);
	}


	/**
	 * 编辑评论列表
	 */
	public function comment_e(Request $req, $id)
	{
        // 保存/新增
        if ($req->method() == 'POST') {
        	return ;
        }
	}

}
